package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.StratumData;
import com.css.fxfzaqbz.modules.data.queryParams.StratumDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.StratumDataRepository;
import com.css.fxfzaqbz.modules.data.service.StratumDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class StratumDataServiceImpl implements StratumDataService {
    @Resource
    StratumDataRepository stratumDataRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public StratumData updateStratumData(ActiveFaultData activeFaultData) {
//
//        activeFaultData.setAfdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
//
//        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
//    }

    @Override
    @Transactional
    public StratumData saveStratumData(StratumData stratumData) {
        stratumData.setSdId(UUIDGenerator.getUUID());
        stratumData.setSdStatus(YNEnum.Y.toString());
        stratumData.setSdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return stratumDataRepository.saveStratumData(stratumData);
    }

    @Override
    public JSONObject getAllStratumData(StratumDataQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(stratumDataRepository.getAllStratumDataParams(queryParams));
    }

    @Override
    public StratumData getStratumDataById(String id) {
        StratumData stratumData = stratumDataRepository.getStratumDataById(id);
        return stratumData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStratumDatas(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        stratumDataRepository.deleteStratumDatasById(ids,timestamp);
    }

    @Override
    public List<StratumData> getAll() {

        return stratumDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<StratumData> stratumData = FileUtil2.importExcel(file, StratumData.class);
        for(int i = 0 ; i<stratumData.size();i++){
            stratumData.get(i).setSdId(UUIDGenerator.getUUID());
            stratumData.get(i).setSdStatus(YNEnum.Y.toString());
            stratumData.get(i).setSdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        }
        stratumDataRepository.importExcel(stratumData);
    }
}
